<?php

use Drupal\shusy_promote\Entity\ShusyPromote;

function shusy_promote_shusy_promote_entity_form($form, &$form_state, ShusyPromote $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_promote') . '/shusy_promote.forms.inc';
  $form_state[ShusyPromote::class] = $e;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('姓名'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];
  $form['phone'] = [
    '#type' => 'textfield',
    '#title' => t('联系电话'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $e->phone,
  ];
  $form['note'] = [
    '#type' => 'textarea',
    '#title' => t('审核备注'),
    '#default_value' => $e->note,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
  ];
  $form['team'] = [
    '#type' => 'checkbox',
    '#title' => t('允许申请团长'),
    '#default_value' => $e->team,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_promote_shusy_promote_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPromote::class];
  if ($e instanceof ShusyPromote) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_promote_shusy_promote_delete_form($form, &$form_state, ShusyPromote $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_promote') . '/shusy_promote.forms.inc';
  $form_state[ShusyPromote::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_promote/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_promote_shusy_promote_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPromote::class];
  if ($e instanceof ShusyPromote) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_promote';
}

function shusy_promote_clear_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_promote') . '/shusy_promote.forms.inc';
  return confirm_form($form,
    t('确认清空所有推广申请数据?'),
    'admin/content/shusy_promote',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_promote_clear_form_submit($form, &$form_state) {
  db_truncate('shusy_promote')->execute();
  db_truncate('shusy_promote_revision')->execute();
  $form_state['redirect'] = 'admin/content/shusy_promote';
}
